package org.jsbd.boss.action;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.common.ExportExcel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.common.ReturnJsonCode;
import org.jsbd.boss.domian.channel.ApkTaskInfo;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.TaskQueryVo;
import org.jsbd.boss.service.IChannelRetentService;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import edu.hziee.common.lang.DateUtil;

/**
 * 
 * 新增任务查询
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/newTask")
public class NewTaskQueryController {
	public static final Logger LOGGER = LoggerFactory.getLogger(NewTaskQueryController.class);

	@Resource
	private IChannelRetentService channelRetentService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String selectNewTaskInfos() {
		return "/newtask/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectNewTaskInfos(TaskQueryVo queryVo, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ApkTaskInfo> list = channelRetentService.selectCountNewTask(queryVo);
		int total = 0;
		for (ApkTaskInfo info : list) {
			total += info.getExecTotal();
		}
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	@RequestMapping(value = "/export", method = RequestMethod.POST)
	@ResponseBody
	public void exportNewTaskInfos(TaskQueryVo queryVo, HttpServletResponse response) throws Exception{
		List<ApkTaskInfo> list = channelRetentService.selectCountNewTask(queryVo);
		Integer total = list.size();

		JSONObject resultJson = new JSONObject();
		String size = PropertiesUtil.getValue("excel_max_size");
		if (total == 0) {
			resultJson.put(ReturnJsonCode.RETURN_CODE, ReturnJsonCode.MsgCodeEnum.EXCEL_EXPORT_ERROR.getCode());
			resultJson.put(ReturnJsonCode.RETURN_MSG, "操作失败！该范围内没有数据。");
			ResponseUtils.renderJson(response, resultJson.toString());
			return;
		}
		if (total > Integer.parseInt(size)) {
			resultJson.put(ReturnJsonCode.RETURN_CODE, ReturnJsonCode.MsgCodeEnum.EXCEL_EXPORT_ERROR.getCode());
			resultJson.put(ReturnJsonCode.RETURN_MSG, "操作失败！查询数据超过excel可导出上限。");
			ResponseUtils.renderJson(response, resultJson.toString());
			return;
		}
		for(ApkTaskInfo apkTaskInfo:list){
			apkTaskInfo.getMobileRate();
			apkTaskInfo.getPayratio();
			apkTaskInfo.getExecRate();
		}
		response.setContentType("octets/stream");
		String filename = DateUtil.formatDate(new Date()) + "新增查询列表.xlsx";
		response.addHeader("Content-Disposition", "attachment;filename="
				+ new String(filename.getBytes("gb2312"), "iso8859-1"));

		ExportExcel<ApkTaskInfo> excel = new ExportExcel<ApkTaskInfo>();

		String[] headers = { "apk名称","应用标识","天 ","新增任务量"," 有手机量 ","已执行量 ","已执行占比 ","手机占比 ","支付次数 ","支付比例 "};

		OutputStream out = response.getOutputStream();

		String title = "新增查询列表";

		excel.exportExcel(title, headers, list, out, "yyyy-MM-dd");

		out.close();
	}
}
